package com.thinkgsp.jxc.persistence.mybatis;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;

/**
 * Mybaits 类型转换类,用于Boolean与varchar('Y';'N')的类型转换
 * @author evan
 *
 */
@MappedJdbcTypes(JdbcType.CHAR)
public class BooleanStringTypeHandler extends BaseTypeHandler<Boolean> {

	@Override
	public Boolean getNullableResult(ResultSet arg0, String arg1)
			throws SQLException {
		// TODO Auto-generated method stub
		String val = arg0.getString(arg1);
		Boolean res = Boolean.FALSE;
		if("Y".equals(val)) {
			res = Boolean.TRUE;
		}
		return res;
	}

	@Override
	public Boolean getNullableResult(ResultSet arg0, int arg1)
			throws SQLException {
		String val = arg0.getString(arg1);
		Boolean res = Boolean.FALSE;
		if("Y".equals(val)) {
			res = Boolean.TRUE;
		}
		return res;
	}

	@Override
	public Boolean getNullableResult(CallableStatement arg0, int arg1)
			throws SQLException {
		String val = arg0.getString(arg1);
		Boolean res = Boolean.FALSE;
		if("Y".equals(val)) {
			res = Boolean.TRUE;
		}
		return res;
	}

	@Override
	public void setNonNullParameter(PreparedStatement arg0, int arg1,
			Boolean arg2, JdbcType arg3) throws SQLException {
		String val = (arg2 ? "Y" : "N");
		arg0.setString(arg1, val);
	}



}
